package com.olymtech.jbricks.jbricks.service.impl;

import com.olymtech.jbricks.jbricks.model.User;
import com.olymtech.jbricks.jbricks.service.CacheDemoService;
import com.olymtech.jbricks.threadlocal.cache.ThreadLocalCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;

/**
 * @author haoting.wang
 * @date 2020/10/23 10:12 上午
 **/
@Service
public class CacheDemoServiceImpl implements CacheDemoService {


    private Random random = new Random(20);

    @Autowired
    private CacheDemoService cacheDemoService;
    @Override
//    @ThreadLocalCache(key = "#id")
    @Cacheable(cacheNames = "test",key = "#id",condition = "#id > 10")
    public User getById(Long id) {

        User user = new User();
        user.setAge(random.nextInt());
        user.setName("wanghaoting");
        user.setBirthday(new Date());
        return user;
    }

    @Override
    public User testLocalCache() {

        User byId = cacheDemoService.getById(1L);
        User byId1 = cacheDemoService.getById(1L);
        System.out.println(byId == byId1);
        return getById(2L);
    }
}
